package me.zhengjie.tools.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import me.zhengjie.common.utils.JacksonUtil
import org.hibernate.annotations.CreationTimestamp
import jakarta.persistence.*
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.data.redis.core.RedisHash
import java.sql.Timestamp
/**
 * sm.ms图床
 *
 * @author Kuki Wu
 * @date 2018-12-27
 */
@Entity
@Table(name = "sys_picture")
@JsonTypeName("Picture")
@RedisHash("picture")
data class Picture (
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,
    var filename: String? = null,
    var url: String? = null,
    var size: String? = null,
    var height: String? = null,
    var width: String? = null,

    @Column(name = "delete_url")
    var delete: String? = null,
    var username: String? = null,

    @CreationTimestamp
    @Column(name = "create_time")
    var createTime: Timestamp? = null,

    // 用于检测文件是否重复
    var md5Code: String? = null

) {
    annotation class Update

    companion object {
        fun fromJson(json: String): Picture? {
            val obj=JacksonUtil.parseObject(json, Picture::class.java)!!
            return when(obj){
                is Picture -> obj
                else -> null
            }
        }
    }
    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }

    fun copy(source: Picture?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}